package com.walsai.pcsys.web.controller;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.walsai.pcsys.core.model.User;

public class SolutionsController extends AbstractController {
	// Este controlador debe busca el archivo solucion que se le pasa como
	// referencia

	// private GenericManager<Problem, Long> problemManager;

	/*
	 * public void setProblemManager(GenericManager<Problem, Long>
	 * problemManager){ this.problemManager = problemManager; }
	 */

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String lineaTxt;
		User u = (User) request.getSession().getAttribute("userInfo");
		String fileName = request.getParameter("problemId") + "-"
				+ u.getUsername() + ".sol";
		ArrayList<String> code = new ArrayList<String>();
		int lineas = 1;

		try {
			BufferedReader sarchivo = new BufferedReader(new FileReader(
					"target/web-1.0-SNAPSHOT/solutions/" + fileName));
			while ((lineaTxt = sarchivo.readLine()) != null) {
				lineas++;
				code.add(lineaTxt);
			}
		} catch (IOException exc) {
			System.err.println(exc);
			System.out
					.println("Asegurese de haber proporcionado el path correcto"
							+ "de la solucion");
		}

		return new ModelAndView("solutionView", "solutionModel", code);

	}
}
